<?php
/**
 * Created by PhpStorm.
 * User: 三星
 * Date: 2018/4/13
 * Time: 22:14
 */

namespace User\Controller;

use Common\Controller\MemberbaseController;

class RentalController extends MemberbaseController
{

    public function reduce()
    {
        $cardnum = I('cardnum');
        if ($cardnum) {
            $userinfo = D('userinfo')->relation(true)->where(array('cardnum' => $cardnum))->find();

            if ($userinfo) {
                $this->assign('userinfo', $userinfo);
                $this->assign('cardnum', $cardnum);
                $this->display();
            } else {
                $this->error('请输入正确卡号！');
            }
        } else {
            $this->assign('cardnum', '');
            $this->display();
        }
    }

    public function doreduce(){
        if ($_POST) {
            $cardnum = I('cardnum');
            $name = I('name');
            $rentmoney = I('rentmoney',0,'float');
            $remark = I('remark');
            $renttime = I('renttime');
            $totalrentmoney = I('totalrentmoney',0,'float');
            //6.23更正信息， 返现的金额可以为负数，在某些情况下可以更新信息（删除金额判断小于0）

            $rentmoney = $rentmoney * 100;
            $totalrentmoney = $rentmoney + $totalrentmoney;

            $data = array(
                'cardnum'   => $cardnum,
                'name'      => $name,
                'rentmoney' => $rentmoney,
                'renttime' => $renttime,
                'remark'    => $remark,
                'createby'  => $this->user['id'],
                'createbyuser'=> $this->user['user_nicename'],
                'createtime'=>date('Y-m-d H:i:s'),
            );

            //添加成功后需要向主表中写入新的数据
            if(M('rental')->add($data)){
                M('userinfo')->where(array('cardnum'=>$cardnum))->save(array('totalrentmoney'=>$totalrentmoney));
                $this->success('租赁费返还成功！',UU('User/Info/index',array('cardnum'=>$cardnum)));
            }

        }else{
            $this->error('请提交正确信息！！');
        }
    }

    public function rentalhis(){
        $cardnum = I('cardnum');
        $name = I('name');
        $data = array('1'=>'1');
        $condition = array();

        if($name){
            $data['name'] = array('like','%'.$name.'%');
            $condition['name'] = $name;
        }

        if($cardnum){
            $data['cardnum'] = $cardnum;
            $condition['cardnum'] = $cardnum;
        }

        if($this->user['sjquanxian']==1){
            $data['createby'] = $this->user['id'];
        }

        $count = D('rental')->where($data)->count();
        $page = $this->page($count,10);

        $hislist = D('rental')->where($data)->order('createtime desc')->limit($page->firstRow.','.$page->listRows)->select();

        $this->assign('hislist',$hislist);
        $this->assign('condition',$condition);
        $this->assign('page',$page->show());
        $this->display();
    }

}